//2009/09/04 12:07:58
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

class RecurrenceRelation
{
public:
    int moduloTen(vector <int> coefficients, vector <int> initial, int N)
    {
		for(int i=0; i<initial.size(); i++) initial[i] = (10 + initial[i]%10) %10;
		for(int i = initial.size(); i<=N; i++)
		{
			int total = 0;
			for(int j=0; j<coefficients.size(); j++)
			{
				total += ((coefficients[coefficients.size() - 1 - j] * initial[initial.size() - 1 - j]) % 10 + 10) % 10;
			}
			initial.push_back((total%10 + 10) % 10);
		}
		return initial[N];
    }
};
